package com.csy.user.controller;


import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.page.PageResult;
import com.csy.user.entity.CarPark;
import com.csy.user.service.ICarParkService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/system/carPark")
public class CarParkController extends BaseController {
    @Resource
    private ICarParkService carParkService;

    /**
     * 查询汽车到闸信息列表
     * @param carPark
     * @return
     */
    @GetMapping("/list")
    public PageResult list(CarPark carPark){
        startPage();
        List<CarPark> list = carParkService.select(carPark);
        return getData(list);
    }

    /**
     * 查询汽车到闸信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public BaseResponse<CarPark> getById(@PathVariable("id") Integer id){
        return BaseResponse.success(carParkService.getById(id));
    }

    /**
     * 根据车牌号查询
     * @param carNumber
     * @return
     */
    @GetMapping("/getByCarNumber/{carNumber}")
    public BaseResponse<CarPark> getByCarNumber(@PathVariable("carNumber") String carNumber){
        CarPark carPark = new CarPark();
        carPark.setCarNumber(carNumber);
        return BaseResponse.success(carParkService.select(carPark).get(0));
    }

    /**
     * 新增
     * @param carPark
     * @return
     */
    @PostMapping
    public BaseResponse<Integer> add(@RequestBody CarPark carPark){
        boolean b = carParkService.save(carPark);
        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 更新
     * @param carPark
     * @return
     */
    @PutMapping
    public BaseResponse<Integer> update(@RequestBody CarPark carPark){
        LocalDateTime leaveTime = carPark.getLeaveTime();
        LocalDateTime enterTime = carPark.getEnterTime();
        Duration between = Duration.between(enterTime, leaveTime);
        long l = between.toHours();
        carPark.setCost(new BigDecimal(l*0.5));
        boolean b = carParkService.updateById(carPark);

        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public BaseResponse<Integer> del(@PathVariable("id")Integer id){
        boolean b = carParkService.removeById(id);
        return toAjax(b == true ? 1 : 0);
    }
}
